<template>
  <div class="test-container">
    <h2>种母分娩API测试</h2>
    
    <el-card class="test-card">
      <template #header>
        <span>测试数据</span>
      </template>
      
      <el-form :model="testForm" label-width="120px">
        <el-form-item label="种母耳号">
          <el-input v-model="testForm.EweEarTag" placeholder="请输入种母耳号" />
        </el-form-item>
        
        <el-form-item label="分娩日期">
          <el-date-picker v-model="testForm.LambingDate" type="date" placeholder="请选择分娩日期" />
        </el-form-item>
        
        <el-form-item label="妊娠天数">
          <el-input-number v-model="testForm.GestationDays" :min="0" />
        </el-form-item>
        
        <el-form-item label="羔总羔数">
          <el-input-number v-model="testForm.TotalLambCount" :min="0" />
        </el-form-item>
        
        <el-form-item label="活羔总数">
          <el-input-number v-model="testForm.LiveLambCount" :min="0" />
        </el-form-item>
        
        <el-form-item label="死羔总数">
          <el-input-number v-model="testForm.DeadLambCount" :min="0" />
        </el-form-item>
      </el-form>
      
      <div class="button-group">
        <el-button type="primary" @click="testCreate">测试新增</el-button>
        <el-button type="success" @click="testQuery">测试查询</el-button>
      </div>
    </el-card>
    
    <el-card class="result-card">
      <template #header>
        <span>测试结果</span>
      </template>
      
      <div class="result-content">
        <h4>请求数据:</h4>
        <pre>{{ requestData }}</pre>
        
        <h4>响应结果:</h4>
        <pre>{{ responseData }}</pre>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { addEweLambing, getEweLambingList } from '@/utils/requestEweLambing'

const testForm = reactive({
  EweEarTag: 'TEST001',
  LambingDate: '',
  GestationDays: 150,
  TotalLambCount: 2,
  LiveLambCount: 2,
  DeadLambCount: 0,
  LambingDifficulty: '顺产',
  MaternalInstinct: '1分',
  Lactation: '1分',
  Mastitis: '无',
  TotalLitterWeight: 5.5,
  LiveLambWeight: 5.5,
  AvgLiveLambWeight: 2.75,
  DeadLambWeight: 0,
  AvgDeadLambWeight: 0,
  FatLambBatch: '',
  HealthyLambCount: 2,
  WeakLambCount: 0,
  DeformedLambCount: 0,
  StillbirthCount: 0,
  AbnormalFetusCount: 0,
  MummifiedFetusCount: 0,
  LitterNumber: 'L001',
  Gender: '公',
  CurrentPen: '1',
  CurrentStall: '1',
  TransferInPen: '',
  TransferInStall: '',
  TransferOutPen: '',
  ResponsiblePerson: '测试员',
  DocumentNumber: 'DOC001',
  Status: '待审核',
  Creator: 'admin',
  Remark: '测试数据'
})

const requestData = ref('')
const responseData = ref('')

const testCreate = async () => {
  try {
    // 处理日期格式
    let formattedDate = ''
    if (testForm.LambingDate) {
      if (typeof testForm.LambingDate === 'string') {
        formattedDate = testForm.LambingDate
      } else if (testForm.LambingDate && typeof testForm.LambingDate === 'object' && 'toISOString' in testForm.LambingDate) {
        formattedDate = (testForm.LambingDate as Date).toISOString().split('T')[0]
      } else {
        formattedDate = String(testForm.LambingDate)
      }
    }
    
    const data = {
      ...testForm,
      LambingDate: formattedDate
    }
    
    requestData.value = JSON.stringify(data, null, 2)
    
    console.log('测试新增API:', data)
    
    const res = await addEweLambing(data)
    responseData.value = JSON.stringify(res.data, null, 2)
    
    if (res.data && res.data.code === 200) {
      ElMessage.success('测试新增成功')
    } else {
      ElMessage.error(res.data?.msg || '测试新增失败')
    }
  } catch (error: any) {
    console.error('测试新增失败:', error)
    responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('测试新增失败')
  }
}

const testQuery = async () => {
  try {
    const params = {
      PageIndex: 1,
      PageSize: 10
    }
    
    requestData.value = JSON.stringify(params, null, 2)
    
    console.log('测试查询API:', params)
    
    const res = await getEweLambingList(params)
    responseData.value = JSON.stringify(res.data, null, 2)
    
    if (res.data && res.data.code === 200) {
      ElMessage.success('测试查询成功')
    } else {
      ElMessage.error(res.data?.msg || '测试查询失败')
    }
  } catch (error: any) {
    console.error('测试查询失败:', error)
    responseData.value = JSON.stringify(error.response?.data || error.message, null, 2)
    ElMessage.error('测试查询失败')
  }
}
</script>

<style scoped>
.test-container {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.test-card {
  margin-bottom: 20px;
}

.result-card {
  margin-bottom: 20px;
}

.button-group {
  margin-top: 20px;
  text-align: center;
}

.button-group .el-button {
  margin: 0 10px;
}

.result-content {
  background: #f8f9fa;
  padding: 15px;
  border-radius: 4px;
}

.result-content pre {
  background: #fff;
  padding: 10px;
  border-radius: 4px;
  border: 1px solid #e9ecef;
  overflow-x: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.result-content h4 {
  margin: 10px 0 5px 0;
  color: #495057;
}
</style> 